﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SrWinGui
{
    public partial class frmOptions : Form
    {
        public frmOptions()
        {
            InitializeComponent();
        }

        private void cmdOk_Click(object sender, EventArgs e)
        {
            UpdateSettings();
        }

        private void UpdateSettings()
        {
            Impostazioni i = Impostazioni.Instance;
            #region TabConnections
            i.Set("DontAutoReconnect",!uxDontAutoReconnect.Checked);
            i.Set("CreateRelayStream", uxCreateRelayStream.Checked, uxCreateRelayStreamExt.Text);
            i.Set("MaxConnToRelay", uxMaxConnToRelay.Checked, uxMaxConnToRelayExt.Text);
            i.Set("TimeOut", uxTimeOut.Checked, uxTimeOutExt.Text);
            i.Set("UseDifferentUserAgent", uxUseDifferentUserAgent.Checked, uxUseDifferentUserAgentExt.Text);                        
            //check if it's requested proxy
            if (uxUseHttpProxy.Text == "")
                i.Set("UseHttpProxy", false, uxUseHttpProxy.Text);
            else
                i.Set("UseHttpProxy", true, uxUseHttpProxy.Text);
            #endregion

            #region TabFile
            if (uxDestinationDir.Text == "")
                i.Set("DestinationDir",false);
            else
                i.Set("DestinationDir",true,uxDestinationDir.Text);

            i.Set("DontWriteIndividualTracks", !uxDontWriteIndividualTracks.Checked);
            i.Set("RipToSingleFile", uxRipToSingleFile.Checked, uxRipToSingleFileExt.Text);
            
            if (uxOverWriteTracksInCompleteDir.Text == "" || uxOverWriteTracksInCompleteDir.Text == "When Larger")
                i.Set("OverWriteTracksInCompleteDir", false, "Larger");
            else
                i.Set("OverWriteTracksInCompleteDir", true, uxOverWriteTracksInCompleteDir.Text);
            i.Set("DontOverwriteTracksInIncomplete", uxDontOverwriteTracksInIncomplete.Checked);
            i.Set("DontCreateDirForEachStream", !uxDontCreateDirForEachStream.Checked);
            i.Set("AddSequenceNumberToOutputFile", uxAddSequenceNumberToOutputFile.Checked, uxAddSequenceNumberToOutputFileExt.Text);
            #endregion

            #region TabPattern
            i.Set("WriteFilesWithPattern", uxWriteFilesWithPattern.Checked, uxWriteFilesWithPatternExt.Text);
            #endregion

            #region TabSplitting
            i.Set("XsOffset", true, uxXsOffset.Text);
            i.Set("XsSilence", true, uxXsSilence.Text);
            i.Set("XsSearchPre", true, uxXsSearchPre.Text);
            i.Set("XsSearchPost", true, uxXsSearchPost.Text);
            i.Set("XsPaddingPre", true, uxXsPaddingPre.Text);
            i.Set("XsPaddingPost", true, uxXsPaddingPost.Text);
            #endregion

            #region TabCodeset
            i.Set("RunExternalCommandForMetaData", uxRunExternalCommandForMetaData.Checked, uxRunExternalCommandForMetaDataExt.Text);
            i.Set("XsCodeSetMeta", uxXsCodeSetMeta.Checked, uxXsCodeSetMetaExt.Text);
            i.Set("XsCodeSetRelay", uxXsCodeSetRelay.Checked, uxXsCodeSetRelayExt.Text);
            i.Set("XsCodeSetId3", uxXsCodeSetId3.Checked, uxXsCodeSetId3Ext.Text);
            i.Set("XsCodeSetFileSys", uxXsCodeSetFileSys.Checked, uxXsCodeSetFileSysExt.Text);
            #endregion

            #region TabMisc
            i.Set("SkipFirstXTracks", uxSkipFirstXTracks.Checked, uxSkipFirstXTracksExt.Text);
            i.Set("RunForNumberOfSeconds", uxRunForNumberOfSeconds.Checked, uxRunForNumberOfSecondsExt.Text);
            i.Set("StopRippingAfterXMb",uxStopRippingAfterXMb.Checked,uxStopRippingAfterXMbExt.Text);
            i.Set("SaveDebug", uxSaveDebug.Checked);
            #endregion

            #region xxx
            i.Save();
            this.DialogResult = DialogResult.OK;


            //TODO:finish adding these options
            //DontScanForFreePorts
            //TruncateDuplicatedTracks
            //DontAddID3Tags
            //AddID3V1
            //DontAddID3v2
            #endregion

        }

        private void frmOpzioni_Load(object sender, EventArgs e)
        {
            updateControls();
        }

        private void updateControls()
        {
            Impostazioni i = Impostazioni.Instance;
            String extValue = "";

            #region TabConnection
            uxDontAutoReconnect.Checked = !i.GetBoolValue("DontAutoReconnect");

            uxCreateRelayStream.Checked = i.GetExtValue("CreateRelayStream", ref extValue);
            uxCreateRelayStreamExt.Text = extValue;
            uxCreateRelayStreamExt.ReadOnly = !uxCreateRelayStream.Checked;

            uxMaxConnToRelay.Checked = i.GetExtValue("MaxConnToRelay", ref extValue);
            uxMaxConnToRelayExt.Text = extValue;
            uxMaxConnToRelayExt.ReadOnly = !uxMaxConnToRelay.Checked;

            uxTimeOut.Checked = i.GetExtValue("TimeOut", ref extValue);
            uxTimeOutExt.Text = extValue;
            uxTimeOutExt.ReadOnly = !uxTimeOut.Checked;

            uxUseDifferentUserAgent.Checked = i.GetExtValue("UseDifferentUserAgent", ref extValue);
            uxUseDifferentUserAgentExt.Text = extValue;
            uxUseDifferentUserAgentExt.Enabled = uxUseDifferentUserAgent.Checked;

            i.GetExtValue("UseHttpProxy", ref extValue);
            uxUseHttpProxy.Text = extValue;
            #endregion

            #region TabFile
            if (i.GetExtValue("DestinationDir",ref extValue))
                uxDestinationDir.Text = extValue;            

            uxDontWriteIndividualTracks.Checked = !i.GetBoolValue("DontWriteIndividualTracks");

            uxRipToSingleFile.Checked = i.GetExtValue("RipToSingleFile", ref extValue);
            uxRipToSingleFileExt.Text = extValue;
            uxRipToSingleFileExt.ReadOnly = !uxRipToSingleFile.Checked;

            i.GetExtValue("OverWriteTracksInCompleteDir", ref extValue);

            uxOverWriteTracksInCompleteDir.SelectedItem = extValue;
            uxDontOverwriteTracksInIncomplete.Checked = i.GetBoolValue("DontOverwriteTracksInIncomplete");
            uxDontCreateDirForEachStream.Checked = !i.GetBoolValue("DontCreateDirForEachStream");

            uxAddSequenceNumberToOutputFile.Checked = i.GetExtValue("AddSequenceNumberToOutputFile", ref extValue);
            uxAddSequenceNumberToOutputFileExt.Text = extValue;
            uxAddSequenceNumberToOutputFileExt.ReadOnly = !uxAddSequenceNumberToOutputFile.Checked;
            #endregion TabFile

            #region TabPattern
            uxWriteFilesWithPattern.Checked = i.GetExtValue("WriteFilesWithPattern", ref extValue);
            uxWriteFilesWithPatternExt.ReadOnly = !uxWriteFilesWithPattern.Checked;
            uxWriteFilesWithPatternExt.Text = extValue;
            #endregion

            #region TabSplitting
            uxXsOffset.Text = i.GetStringValue("XsOffset");
            uxXsSilence.Text = i.GetStringValue("XsSilence");
            uxXsSearchPre.Text = i.GetStringValue("XsSearchPre");
            uxXsSearchPost.Text = i.GetStringValue("XsSearchPost");
            uxXsPaddingPre.Text = i.GetStringValue("XsPaddingPre");
            uxXsPaddingPost.Text = i.GetStringValue("XsPaddingPost");
            #endregion

            #region TabCodeSet
            uxRunExternalCommandForMetaData.Checked = i.GetExtValue("RunExternalCommandForMetaData", ref extValue);
            uxRunExternalCommandForMetaDataExt.Text = extValue;
            uxRunExternalCommandForMetaDataExt.ReadOnly = !uxRunExternalCommandForMetaData.Checked;

            uxXsCodeSetMeta.Checked = i.GetExtValue("XsCodeSetMeta", ref extValue);
            uxXsCodeSetMetaExt.Enabled = uxXsCodeSetMeta.Checked;
            uxXsCodeSetMetaExt.SelectedItem = extValue;

            uxXsCodeSetRelay.Checked = i.GetExtValue("XsCodeSetRelay", ref extValue);
            uxXsCodeSetRelayExt.Enabled = uxXsCodeSetRelay.Checked;
            uxXsCodeSetRelayExt.SelectedItem = extValue;

            uxXsCodeSetId3.Checked = i.GetExtValue("XsCodeSetId3", ref extValue);
            uxXsCodeSetId3Ext.Enabled = uxXsCodeSetId3.Checked;
            uxXsCodeSetId3Ext.SelectedItem = extValue;

            uxXsCodeSetFileSys.Checked = i.GetExtValue("XsCodeSetFileSys", ref extValue);
            uxXsCodeSetFileSysExt.Enabled = uxXsCodeSetFileSys.Checked;
            uxXsCodeSetFileSysExt.SelectedItem = extValue;
            #endregion

            #region TabMisc
            uxSkipFirstXTracks.Checked = i.GetExtValue("SkipFirstXTracks", ref extValue);
            uxSkipFirstXTracksExt.ReadOnly = !uxSkipFirstXTracks.Checked;
            uxSkipFirstXTracksExt.Text = extValue;

            uxRunForNumberOfSeconds.Checked = i.GetExtValue("RunForNumberOfSeconds", ref extValue);
            uxRunForNumberOfSecondsExt.Text = extValue;
            uxRunForNumberOfSecondsExt.ReadOnly = !uxRunForNumberOfSeconds.Checked;

            uxStopRippingAfterXMb.Checked = i.GetExtValue("StopRippingAfterXMb", ref extValue);
            uxStopRippingAfterXMbExt.ReadOnly = !uxStopRippingAfterXMb.Checked;
            uxStopRippingAfterXMbExt.Text = extValue;

            uxSaveDebug.Checked = i.GetBoolValue("SaveDebug");
            #endregion

        }

        #region ckeckbox events
        private void uxCreateRelayStream_CheckedChanged(object sender, EventArgs e)
        {
            uxCreateRelayStreamExt.ReadOnly = !uxCreateRelayStream.Checked;
        }

        private void uxMaxConnToRelay_CheckedChanged(object sender, EventArgs e)
        {
            uxMaxConnToRelayExt.ReadOnly = !uxMaxConnToRelay.Checked;
        }

        private void uxTimeOut_CheckedChanged(object sender, EventArgs e)
        {
            uxTimeOutExt.ReadOnly = !uxTimeOut.Checked;
        }

        private void uxUseDifferentUserAgent_CheckedChanged(object sender, EventArgs e)
        {
            uxUseDifferentUserAgentExt.Enabled = uxUseDifferentUserAgent.Checked;
        }

        private void uxRipToSingleFile_CheckedChanged(object sender, EventArgs e)
        {
            uxRipToSingleFileExt.ReadOnly = !uxRipToSingleFile.Checked;
        }

        private void uxAddSequenceNumberToOutputFile_CheckedChanged(object sender, EventArgs e)
        {
            uxAddSequenceNumberToOutputFileExt.ReadOnly = !uxAddSequenceNumberToOutputFile.Checked;
        }

        private void uxWriteFilesWithPattern_CheckedChanged(object sender, EventArgs e)
        {
            uxWriteFilesWithPatternExt.ReadOnly = !uxWriteFilesWithPattern.Checked;
        }

        private void uxRunExternalCommandForMetaData_CheckedChanged(object sender, EventArgs e)
        {
            uxRunExternalCommandForMetaDataExt.ReadOnly = !uxRunExternalCommandForMetaData.Checked;
        }

        private void uxXsCodeSetMeta_CheckedChanged(object sender, EventArgs e)
        {
            uxXsCodeSetMetaExt.Enabled = uxXsCodeSetMeta.Checked;
        }

        private void uxXsCodeSetRelay_CheckedChanged(object sender, EventArgs e)
        {
            uxXsCodeSetRelayExt.Enabled = uxXsCodeSetRelay.Checked;
        }

        private void uxXsCodeSetId3_CheckedChanged(object sender, EventArgs e)
        {
            uxXsCodeSetId3Ext.Enabled = uxXsCodeSetId3.Checked;
        }

        private void uxXsCodeSetFileSys_CheckedChanged(object sender, EventArgs e)
        {
            uxXsCodeSetFileSysExt.Enabled = uxXsCodeSetFileSys.Checked;
        }

        private void uxSkipFirstXTracks_CheckedChanged(object sender, EventArgs e)
        {
            uxSkipFirstXTracksExt.ReadOnly = !uxSkipFirstXTracks.Checked;
        }

        private void uxRunForNumberOfSeconds_CheckedChanged(object sender, EventArgs e)
        {
            uxRunForNumberOfSecondsExt.ReadOnly = !uxRunForNumberOfSeconds.Checked;
        }

        private void uxStopRippingAfterXMb_CheckedChanged(object sender, EventArgs e)
        {
            uxStopRippingAfterXMbExt.ReadOnly = !uxStopRippingAfterXMb.Checked;
        }
        #endregion

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void cmdReset_Click(object sender, EventArgs e)
        {
            //TODO: r u sure
            Impostazioni.Instance.Inizializza();
            updateControls();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                uxDestinationDir.Text = folderBrowserDialog1.SelectedPath;
        }
    }
}